/**
 * 神州信息(2012)
 */
package net.abbp.sms;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.comm.CommDriver;
import javax.swing.JButton;
import javax.swing.JFrame;

import org.smslib.GatewayException;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

/**
 * 短信发送器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：2012-2-4
 */
public enum SmsSender {
    INSTANCE;

    public final static String MSG_FILE_PATH = "modem.properties";

    private SmsSender() {
        // this.initService();
    }

    /**
     * 发送短信
     * 
     * @param mobile
     *            手机号码
     * @param message
     *            消息内容
     * @return 发送状态
     */
    public boolean sendMsg(String mobile, String message) {
        OutboundMessage msg = new OutboundMessage(mobile, message);
        msg.setEncoding(MessageEncodings.ENCUCS2);

        try {
            Service.getInstance().sendMessage(msg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JButton btn = new JButton("Test");
        frame.add(btn);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

        SmsSender.INSTANCE.initService();
        frame.setVisible(true);
    }

    /**
     * 102
     * 初始化
     */
    public void initService() {
        String gatewayId = "modem.com1";
        String comPort = "COM3";
        int baudRate = 9600;
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("mtr.properties"));

            gatewayId = properties.getProperty("gatewayId");
            comPort = properties.getProperty("com_msg");
            baudRate = Integer.parseInt(properties.getProperty("baudRate"));
            System.out.println(baudRate);
            // properties.getProperty("manufacturer");
            // properties.getProperty("model") == null ? "" :
            // properties.getProperty("model");

        } catch (IOException e1) {
            System.out.println(" ======= no: /properties/modem.properties ============ ");
            e1.printStackTrace();
        }

        try {
            System.loadLibrary("win32com");
            String driverName = "com.sun.comm.Win32Driver";
            CommDriver driver;
            driver = (CommDriver) Class.forName(driverName).newInstance();
            driver.initialize();
            System.out.println("使用端口:" + comPort);
            SerialModemGateway gateway = new SerialModemGateway(gatewayId, comPort, baudRate, "Huawei", "");
            gateway.setOutbound(true);
            gateway.setSimPin("0000");

            Service.getInstance().addGateway(gateway);

            Service.getInstance().startService();
            System.out.println();
            System.out.println("Modem Information:");
            System.out.println("  Manufacturer: " + gateway.getManufacturer());
            System.out.println("  Model: " + gateway.getModel());
            System.out.println("  Serial No: " + gateway.getSerialNo());
            System.out.println("  SIM IMSI: " + gateway.getImsi());
            System.out.println("  Signal Level: " + gateway.getSignalLevel() + " dBm");
            System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
            System.out.println();

        } catch (GatewayException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (SMSLibException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
